﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Quotation
{
    /// <summary>
    /// 交易时段信息
    /// </summary>
    public class TimeRangeCollection
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sessions">交易时段集合</param>
        public TimeRangeCollection(IEnumerable<TimeRange> sessions)
        {
            this._sessions = sessions.OrderBy(t => t.StartTime).ToList();
        }

        private List<TimeRange> _sessions;
        /// <summary>
        /// 交易时段集合
        /// </summary>
        public IList<TimeRange> Sessions
        {
            get { return _sessions; }
        }
        /// <summary>
        /// 将字符串解析为交易时段信息
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static TimeRangeCollection Parse(string str)
        {
            if (string.IsNullOrEmpty(str))
                return null;
            var strArray = str.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            if (strArray == null && strArray.Length == 0)
                return null;


            List<TimeRange> sessions = new List<TimeRange>();
            foreach (var sessionStr in strArray)
            {
                var match = Regex.Match(sessionStr, @"(\d{0,2}):(\d{2})-(\d{0,2}):(\d{2})");
                if (match.Success)
                {
                    TimeRange tr = new TimeRange()
                    {
                        StartTime = new Time(int.Parse(match.Groups[1].Value), int.Parse(match.Groups[2].Value)),
                        EndTime = new Time(int.Parse(match.Groups[3].Value), int.Parse(match.Groups[4].Value))
                    };
                    sessions.Add(tr);
                }
                else
                {
                    throw new Exception(string.Format("无法将字符串 {0} 解析为交易时段信息", str));
                }
            }
            TimeRangeCollection rtn = new TimeRangeCollection(sessions);
            return rtn;

        }
        /// <summary>
        /// 重写ToString方法
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (this.Sessions != null)
            {

                List<string> strArray = new List<string>();

                foreach (var s in this.Sessions)
                {
                    string str = string.Format("{0}-{1}", s.StartTime.ToShortString(), s.EndTime.ToShortString());
                    strArray.Add(str);
                }
                return string.Join(",", strArray.ToArray());
            }
            else
                return null;
        }


        public static bool operator ==(TimeRangeCollection a, TimeRangeCollection b)
        {

            if (ReferenceEquals(a, b))
                return true;
            else if ((object)a == null || (object)b == null)
                return false;
            else
                return a.ToString() == b.ToString();
        }
        public static bool operator !=(TimeRangeCollection a, TimeRangeCollection b)
        {

            return !(a == b);
        }
    }
}
